<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">添加文章</el-button>
    </template>
    <!-- 表单 -->
    <!-- inline属性,在一行显示 -->
    <el-form :inline="true">
      <el-form-item label="文章分类:">
        <!-- 绑定 select封装的组件  传递 对应选中的 id的值 -->
        <ChannelSelect v-model="params.cate_id" width="100px"></ChannelSelect>
      </el-form-item>
      <el-form-item label="发布状态:">
        <el-select style="width: 100px" v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置 </el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->

    <el-table :data="articleList" style="width: 100%" v-loading="isLoading">
      <el-table-column label="文章标题" prop="title">
        <!-- 标题变成链接样式   解构出row 拿到一行的数据-->
        <template #default="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="showArticleDetail(row)"
            >{{ row.title }}</el-link
          >
        </template>
      </el-table-column>
      <el-table-column label="发布时间" prop="pub_date">
        <!-- 格式化时间  配置了插槽，会显示插槽内容，忽略prop内容-->
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="文章状态" prop="state"></el-table-column>
      <el-table-column label="文章所属分类" prop="cate_name"></el-table-column>
      <el-table-column label="操作">
        <!-- 作用域插槽解构出row 拿到一行的数据 -->
        <template #default="{ row }">
          <el-button
            circle
            plain
            type="primary"
            :icon="Edit"
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            circle
            plain
            type="danger"
            :icon="Delete"
            @click="onDelArticle(row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[1, 2, 3, 5]"
      :background="true"
      layout="jumper,total, sizes, prev, pager, next "
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />

    <!-- 抽屉 添加和修改 -->
    <ArticleEdit ref="articleEditRef" @success="onSuccess"></ArticleEdit>
  </page-container>
</template>
<script lang="ts" setup name="">
import { Delete, Edit } from '@element-plus/icons-vue'
import { ref } from 'vue'
import ChannelSelect from '@/components/ChannelSelect.vue'
import { artGetListService, artDelService } from '@/api/article'
import { formatTime } from '@/utils/format'
import ArticleEdit from '@/components/ArticleEdit.vue'

const articleList = ref([
  {
    id: 5961,
    title: '新的文章啊1',
    pub_date: '2022-07-10 14:53:52.604',
    state: '已发布',
    cate_name: '体育'
  },
  {
    id: 5962,
    title: '新的文章啊2',
    pub_date: '2022-07-10 14:54:30.904',
    state: '草稿',
    cate_name: '体育'
  }
])

//加载效果
const isLoading = ref(false)

//select选择的id
//定义请求参数对象
const params = ref({
  pagenum: 1,
  pagesize: 3,
  cate_id: '',
  state: '已发布'
})

const total = ref(0) //总条数

//获取文章列表
const getArticle = async () => {
  isLoading.value = true
  const res = await artGetListService(params.value)
  // console.log(res)
  articleList.value = res.data.data
  total.value = res.data.total
  isLoading.value = false
}
getArticle()

// 处理分页
const onSizeChange = (size: any) => {
  //当前分页条数更新为size
  // console.log(size)
  params.value.pagesize = size
  //从第一页重新渲染  防止展示条数增加  当前页不存在
  params.value.pagenum = 1
  getArticle()
}
const onCurrentChange = (page: any) => {
  //当前分页 变为 第 page 页
  // console.log(page)
  params.value.pagenum = page
  getArticle()
}

//搜索  重置
//搜索 =》 按照最新的条件重新检索  从第一页开始展示
const onSearch = () => {
  params.value.pagenum = 1
  getArticle()
}
const onReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticle()
}

//添加
const onAddArticle = () => {
  articleEditRef.value.open({}) //调用open方法，控制显示
  //传一个空对象，说明是添加
}
//修改   row 是要改的行的数据
const onEditArticle = (row: any) => {
  // console.log(row.id)
  articleEditRef.value.open(row) //调用open方法，控制显示  并传入数据，用于编辑
}

const onDelArticle = async (id: number) => {
  const res = await artDelService(id.toString())
  // console.log(res)
  ElMessage.success(res.data.message)
  getArticle() //重新渲染
}

const articleEditRef = ref() //抽屉

//添加或者编辑成功
const onSuccess = (type: string) => {
  if (type === 'add') {
    //添加  渲染最后一页  方便看到添加的内容
    //计算最后一页页码  并且向上取整
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  } else if (type === 'edit') {
    //修改  渲染当前页
    //直接做渲染 不需要处理
  }
  getArticle()
}

const showArticleDetail = (row: any) => {
  articleEditRef.value.open(row)
}
</script>

<style scoped lang="scss"></style>
